{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, BatchNormalization, merge\n",
    "from keras import backend as K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=8):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'inputs': [{'shape': [8, 8, 2], 'data': [-0.64198911, 0.98544611, 0.0413525, -0.44193272, 0.9624069, 0.72663929, 0.00034972, 0.51540945, 0.63127325, -0.89787762, 0.47697471, 0.55533985, -0.27374483, -0.96484403, 0.91351482, -0.26913556, 0.71642108, -0.61447154, 0.0342177, -0.69129628, -0.31807737, 0.40087754, 0.48301173, -0.08120329, -0.25489111, 0.64229059, 0.24235387, -0.19778677, 0.65878057, 0.00119121, -0.28716175, -0.29611409, 0.02686545, 0.2475214, -0.79332453, 0.85945404, -0.43013664, -0.98394221, 0.23271344, -0.38336772, -0.2279542, 0.73435413, -0.76005096, -0.44495119, 0.53716444, 0.00209122, -0.75949652, -0.84290049, 0.51219718, 0.18040438, -0.59568039, -0.48940408, 0.71061297, 0.9945177, 0.38184722, -0.47563752, 0.71261047, -0.56375239, 0.92222333, -0.80045977, -0.97423491, 0.77069561, -0.67323472, 0.98632911, 0.16356088, -0.51235006, -0.77634239, -0.98981187, 0.30975529, 0.2039768, -0.27577344, 0.01890532, -0.9140824, -0.78281448, 0.0891309, 0.48088098, 0.722558, 0.89490085, -0.77470338, 0.49576982, -0.62339735, 0.55088705, -0.99675968, -0.23529344, -0.26909803, 0.58104596, -0.64607666, -0.37214156, 0.7042701, -0.3850215, 0.27946564, -0.21464435, 0.58755794, 0.46166156, 0.06230831, -0.50981746, -0.95366104, 0.2739038, 0.3694854, -0.83002922, -0.21686653, -0.97678955, 0.59672912, -0.83950693, -0.45179895, -0.96310049, -0.46184114, -0.49785073, 0.9261875, 0.57904538, -0.43003235, 0.7622151, 0.2496891, -0.18537126, -0.65585366, -0.63974591, -0.29207825, 0.65647629, -0.2251891, -0.43513959, -0.90673784, -0.69145196, -0.53783705, -0.12126235, 0.05670955, 0.67572419, -0.22935537, 0.52933974]}, {'shape': [8, 8, 2], 'data': [0.82631798, 0.66604793, -0.21910133, -0.40288226, -0.80102052, -0.40184335, -0.01793001, 0.15278828, 0.59415157, 0.58974892, 0.01527455, 0.59693054, -0.76377429, 0.01605496, 0.35636957, 0.64246584, 0.92787956, 0.17395392, 0.01754594, -0.57830267, 0.525812, 0.27688831, 0.08357316, -0.70066388, -0.11049262, 0.1851951, 0.3530688, 0.7238878, 0.00089533, -0.84381649, 0.13790456, -0.26863065, 0.18105373, 0.9742298, -0.76026754, -0.47622785, 0.78608895, 0.64421613, -0.19070911, -0.93552667, -0.2889369, -0.74733201, -0.5584528, 0.91956277, 0.4383989, -0.61087684, 0.13257913, -0.99458897, -0.97560214, 0.24196904, 0.83309941, 0.88488167, -0.81762003, -0.93043275, 0.62654196, 0.10201481, -0.83502333, 0.67738349, -0.4859239, -0.97002863, -0.89610678, 0.52378416, -0.06729277, -0.75963797, 0.66414206, -0.68960991, 0.95501147, -0.53570326, -0.72428505, 0.31745639, 0.48380151, 0.42199423, 0.65135503, 0.82156772, 0.18165986, -0.66959142, 0.15195927, 0.18181145, 0.66947753, 0.36018469, 0.78914908, 0.71607508, 0.78482611, 0.33188651, -0.97357278, 0.95912056, 0.94509432, 0.20024466, 0.18007749, 0.02625601, 0.45804715, -0.68640212, -0.09990986, -0.38604688, 0.2033138, -0.42340642, -0.18938095, 0.65347321, 0.92147816, -0.91234878, 0.68725265, 0.21972113, -0.09563298, 0.51283223, 0.99214018, -0.49609326, 0.90248298, -0.68546406, -0.46813169, -0.41309103, 0.6096741, 0.40334197, -0.78944123, 0.31476, -0.15709671, 0.48910606, 0.85030791, 0.10421701, -0.45823166, 0.04847702, 0.525014, -0.76785408, -0.80038015, -0.37583558, 0.92245751, -0.63345568, -0.20867703, -0.72200284]}], 'weights': [{'shape': [3, 3, 2, 4], 'data': [-0.64198911, 0.98544611, 0.0413525, -0.44193272, 0.9624069, 0.72663929, 0.00034972, 0.51540945, 0.63127325, -0.89787762, 0.47697471, 0.55533985, -0.27374483, -0.96484403, 0.91351482, -0.26913556, 0.71642108, -0.61447154, 0.0342177, -0.69129628, -0.31807737, 0.40087754, 0.48301173, -0.08120329, -0.25489111, 0.64229059, 0.24235387, -0.19778677, 0.65878057, 0.00119121, -0.28716175, -0.29611409, 0.02686545, 0.2475214, -0.79332453, 0.85945404, -0.43013664, -0.98394221, 0.23271344, -0.38336772, -0.2279542, 0.73435413, -0.76005096, -0.44495119, 0.53716444, 0.00209122, -0.75949652, -0.84290049, 0.51219718, 0.18040438, -0.59568039, -0.48940408, 0.71061297, 0.9945177, 0.38184722, -0.47563752, 0.71261047, -0.56375239, 0.92222333, -0.80045977, -0.97423491, 0.77069561, -0.67323472, 0.98632911, 0.16356088, -0.51235006, -0.77634239, -0.98981187, 0.30975529, 0.2039768, -0.27577344, 0.01890532]}, {'shape': [4], 'data': [0.82631798, 0.66604793, -0.21910133, -0.40288226]}, {'shape': [3, 3, 2, 4], 'data': [-0.34722139, -0.69135848, 0.01789954, 0.47110973, -0.64695267, -0.25135834, -0.84553569, -0.06711232, -0.80116285, -0.60321447, -0.92210462, 0.30857423, -0.93859038, -0.50168069, -0.3443459, 0.52050005, -0.71329717, 0.16468709, 0.725692, -0.04096577, 0.99831966, -0.55034246, 0.66881126, -0.1588944, -0.75326816, -0.54230946, -0.94161241, -0.79970173, -0.04001772, 0.98419785, -0.0637638, -0.2264114, 0.85467243, -0.29737323, -0.35508185, 0.84356731, -0.64841089, -0.90096546, -0.88785204, 0.43163302, 0.10577091, 0.51088149, 0.39467564, 0.60137617, -0.92513424, -0.55207028, 0.17716658, -0.3931455, -0.45943291, -0.36262353, 0.76789976, -0.64596013, 0.03339056, 0.85335633, -0.89373919, -0.8114777, 0.27758422, 0.58802194, 0.6069782, 0.71572374, 0.04142521, 0.0328487, -0.73654655, 0.20876356, 0.63592509, -0.72030378, -0.64853501, 0.62803869, 0.21148697, -0.45838771, 0.70692881, 0.984257]}, {'shape': [4], 'data': [0.22457511, -0.51059572, 0.07144619, -0.26523922]}], 'expected': {'shape': [6, 6, 4], 'data': [0.63588494, 1.56715083, 0.0, 0.85182446, 1.76892912, 0.3518993, 1.28529739, 0.35924459, 1.05547523, 0.47962758, 0.75775194, 0.0, 0.10851398, 0.85821122, 0.89333785, 1.25613761, 0.54342258, 0.26422334, 0.0, 0.78383929, 1.24912131, 2.03046298, 0.16637705, 0.50790149, 0.0, 1.65955675, 0.16812465, 0.0, 0.3224318, 0.0, 0.0, 0.28224164, 2.26121092, 0.76213753, 0.72725958, 0.13807885, 0.22789271, 0.0, 1.57516181, 0.0, 2.17956805, 0.01826537, 0.42274648, 0.25605518, 0.95378268, 0.75267404, 0.0, 1.98346329, 1.47232997, 0.44511241, 1.62310719, 0.69085991, 0.20459607, 0.0, 0.0, 1.04370248, 0.75815439, 1.20114076, 0.0, 1.45118272, 1.17148328, 1.12532985, 0.0, 0.34933943, 0.56182456, 1.80738258, 1.36068761, 0.41810936, 0.28452775, 0.0, 0.70081246, 0.53206635, 0.20675895, 2.22871637, 0.0, 0.0, 0.0, 0.0, 0.67434675, 1.13594329, 2.51270533, 1.07475972, 0.21780843, 0.72954613, 1.26831591, 0.0, 0.14889818, 0.02980909, 1.28569329, 1.09660697, 0.30631244, 0.0, 0.0, 0.22364688, 1.01558316, 0.0, 1.119892, 0.27566165, 0.41100192, 0.0, 0.1657871, 0.0, 1.89906955, 0.29131794, 0.9767828, 0.09414321, 0.35459602, 0.33910602, 0.29520699, 0.69242674, 0.0, 0.52945316, 0.30156654, 0.0, 1.06028497, 0.0, 0.16667619, 0.0, 2.15982008, 1.00657248, 1.95690143, 1.06393909, 0.0, 1.79269028, 0.0, 0.48225194, 0.16820998, 1.40097678, 0.73587751, 1.19495797, 0.4992888, 0.97357053, 0.49398535, 0.95244753, 0.8407464, 1.47925234, 0.43860519, 0.54667789, 0.0, 0.0, 0.05579159, 0.13711736, 0.58056778, 0.55201781]}}\n"
     ]
    }
   ],
   "source": [
    "data_in_shape = (8, 8, 2)\n",
    "\n",
    "input_layer_0 = Input(shape=data_in_shape)\n",
    "branch_0 = Conv2D(4, 3, 3, activation='relu', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)(input_layer_0)\n",
    "\n",
    "input_layer_1 = Input(shape=data_in_shape)\n",
    "branch_1 = Conv2D(4, 3, 3, activation='relu', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)(input_layer_1)\n",
    "\n",
    "output_layer = merge([branch_0, branch_1], mode='ave')\n",
    "model = Model(input=[input_layer_0, input_layer_1], output=output_layer)\n",
    "\n",
    "data_in = []\n",
    "for i in range(2):\n",
    "    np.random.seed(15000 + i)\n",
    "    data_in.append(np.expand_dims(2 * np.random.random(data_in_shape) - 1, axis=0))\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(15000 + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(data_in)\n",
    "\n",
    "print({\n",
    "    'inputs': [{'data': format_decimal(data_in[i].ravel().tolist()), 'shape': list(data_in_shape)} for i in range(2)],\n",
    "    'weights': [{'data': format_decimal(weights[i].ravel().tolist()), 'shape': list(weights[i].shape)} for i in range(len(weights))],\n",
    "    'expected': {'data': format_decimal(result[0].ravel().tolist()), 'shape': list(result[0].shape)}\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
